package com.miplus.ccrm.workflow.nodes;

import com.miplus.ccrm.workflow.logic.LfEdge;
import com.miplus.ccrm.workflow.logic.LfNode;

import java.util.List;

public abstract class BaseV2NodeConverter implements INodeConverter {
    /**
     * Convert node to code 转换节点为代码
     * @param config  节点配置
     * @param lines 连线配置
     * @param nodes 节点配置
     * @param nodeConverters 转换器集合
     * @param flowConverter 流程转换器
     * @return  string 转换后的代码字符串
     */
    @Override
    public String converter(LfNode config, List<LfEdge> lines, List<LfNode> nodes, List<INodeConverter> nodeConverters, IFlowConverter flowConverter) {
        String[] templates = new String[]{
            String.format("async function %s(){",config.getId()),
                "    try {",
                "       "+flowConverter.globalVar(config.getId(),"{}"),// 初始化节点的全局变量
                "       "+flowConverter.printValue("Node "+config.getId()+" Executed"), // 打印节点执行信息
                "       ${body}",
                "    } catch (error) {",
                "       "+flowConverter.print("error"),
                "    }",
            "}",
        };
        return  String.join("\n", templates);
    }
}
